//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System;
using EntLibContrib.Query.Properties;

namespace EntLibContrib.Query
{
	/// <summary>
	/// Abstract Custom Command
	/// </summary>
	/// <remarks>This type holds the name of a custom command
	/// and is responsible for reflecting it on request</remarks>
	public abstract class CustomCommand : ICustomCommand
	{
		#region Fields
		private string commandTypeName;
		private ICommand customCommand;
		#endregion

		#region Properties
		/// <summary>
		/// Gets the custom command instance.
		/// </summary>
		/// <value>The command.</value>
		public ICommand Command
		{
			get
			{
				if (customCommand == null)
					customCommand = (ICommand)Activator.CreateInstance(Type.GetType(commandTypeName, true, true));

				return customCommand;
			}
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="CustomCommand"/> class.
		/// </summary>
		/// <param name="commandTypeName">Name of the type of the command.</param>
		protected CustomCommand(string commandTypeName)
		{
			ValidateAndStoreParameters(commandTypeName);
		}
		#endregion

		#region Private Methods
		/// <summary>
		/// Validates the and store parameters.
		/// </summary>
		/// <param name="customCommandTypeName">Name of the custom command type.</param>
		private void ValidateAndStoreParameters(string customCommandTypeName)
		{
			// check for a null or empty path name
			if (string.IsNullOrEmpty(customCommandTypeName))
				throw new ArgumentNullException("customCommandTypeName", InternalResources.ExceptionCustomCommandTypeNameNullOrEmpty);

			// store the file name
			this.commandTypeName = customCommandTypeName;
		}
		#endregion
	}
}
